<?php
if (!defined("OK")) { header("Location: ../"); }

$timeoutseconds = 300;                 // Timeout sekundemis
if (isset($url['id'])) { $failas = $url['id']; }	//kuriame faile yra
else { $failas = 'home'; }

if (isset($_SERVER['HTTP_REFERER'])) { $ref = $_SERVER['HTTP_REFERER'];	}	//is kur atejo
else { $ref = 'Direct Link'; }

$ip = getip();				//funkcija IP gauti
$timestamp = time();			//timestampas
$timeout = $timestamp-$timeoutseconds;	//taimout aritmetika :)
$db_expire = $timestamp-259200; //3 dienos

if (isset($_SESSION['username']) AND !empty($_SESSION['username'])) {
	$username = $_SESSION['username'];
	$uzerid = $_SESSION['id'];
}
else {
	$username = 'Svečias';
	$uzerid = '0';
}

$test = mysql_fetch_assoc(mysql_query1("SELECT * FROM kas_prisijunges WHERE timestamp>".escape($timeout)." AND ip=".escape($ip)." LIMIT 1"));

if (!isset($_COOKIE["uid"])) {
	$uid = random_name();
	setcookie("uid", $uid, $timestamp + 259200); //3 dienos
	if (isset($test['ip'])) {
		klaida("COOKIES","Perspėjame<br/>Sausainėliai(Cookies) turi būti priimami."); 
		$q = mysql_query1("UPDATE kas_prisijunges SET 
		id=".escape($uzerid).",
		clicks = clicks+1,
		ref=".escape($ref).",
		file=".escape($failas).",
		user=".escape($username).",
		timestamp=".escape($timestamp)."
		WHERE ip=".escape($ip)."");
	}
	else {
		$q = mysql_query1("INSERT INTO kas_prisijunges VALUES (
		".escape($uzerid).",
		".escape($uid).",
		".escape($timestamp).",
		".escape($ip).",
		".escape($failas).",
		".escape($username).",
		".escape($ref).",
		'1'
		)");
	}
}
else {
	$uid = $_COOKIE["uid"];
	$test = mysql_fetch_assoc(mysql_query1("SELECT * FROM kas_prisijunges WHERE uid=".escape($uid)." LIMIT 1"));
	if (isset($test['uid']) && !empty($test['uid'])) {
		$q = mysql_query1("UPDATE kas_prisijunges SET 
		id=".escape($uzerid).",
		ip=".escape($ip).",
		clicks = clicks+1,
		ref=".escape($ref).",
		file=".escape($failas).",
		user=".escape($username).",
		timestamp=".escape($timestamp)."
		WHERE uid=".escape($uid)."");
	}
	else {
		$q = mysql_query1("INSERT INTO kas_prisijunges VALUES (
		".escape($uzerid).",
		".escape($uid).",
		".escape($timestamp).",
		".escape($ip).",
		".escape($failas).",
		".escape($username).",
		".escape($ref).",
		'1'
		)");
	}
}
$q = mysql_query1("DELETE FROM kas_prisijunges WHERE timestamp<".escape($db_expire)."");
$q = mysql_query1("SELECT * FROM kas_prisijunges WHERE timestamp>".escape($timeout)."");
$online = ''; $i = ''; $u = '';
while ($row = @mysql_fetch_assoc($q)) {
	if (!empty($row['id'])) { $i++; }
	else { $u++; }
}
if (isset($u) && $u==1) { $online .= "<a href='?id,40'>$u svečias</a>\n"; }
elseif (isset($u) && $u>1) { $online .= "<a href='?id,40'>$u svečiai</a>\n"; }
if (isset($i) && $i==1) { $online .= "<a href='?id,40'>$i vartotojas</a>\n"; }
elseif (isset($i) && $i>1) { $online .= "<a href='?id,40'>$i vartotojai</a>\n"; }

//////////////////// ONLINE END /////////////////////
?>



